package com.gsafety.library.Widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.gsafety.library.R;
import com.gsafety.library.Exts.Ext;

/**
 * 无数据展示无数据占位图的列表
 */
public class NoDataListView extends ListView
{
    private Paint paint;
    private int no_date_res = -1;
    private int mHeight = 0;
    private int mWidth = 0;
    private int imgHeight = 0;
    private int imgWidth = 0;
    private Bitmap bitmap = null;

    public NoDataListView(Context context)
    {
        this(context, null);
    }

    public NoDataListView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public NoDataListView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setAntiAlias(true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NoDataListView);
        no_date_res = typedArray.getResourceId(R.styleable.NoDataListView_no_data_img, -1);
        typedArray.recycle();
    }

    // 实例化图形
    private Bitmap initBitmap()
    {
        if (no_date_res == -1) return null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 只取属性，不申请内存
        bitmap = BitmapFactory.decodeResource(getResources(), no_date_res, options);
        int outWidth = options.outWidth;
        int outHeight = options.outHeight;
        int inSampleSize = 1;
        // 如果比实际控件大，那么需要缩小，节省内存
        if (outWidth > mWidth || outHeight > mHeight)
        {
            int halfHeight = outHeight / 2;
            int halfWidth = outWidth / 2;
            while ((halfHeight / inSampleSize) > mHeight || (halfWidth / inSampleSize) > mWidth)
            {
                inSampleSize *= 2;
            }
        }
        // 设置缩小比例
        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;
        // 真正申请内存，实例化图形
        bitmap = BitmapFactory.decodeResource(getResources(), no_date_res, options);
        imgWidth = bitmap == null ? 0 : bitmap.getWidth();
        imgHeight = bitmap == null ? 0 : bitmap.getHeight();
        return bitmap;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = Ext.measureWidth(widthMeasureSpec);
        mHeight = Ext.measureHeight(heightMeasureSpec);
    }

    /**
     * 回收资源
     */
    public void recycle()
    {
        if (bitmap != null && !bitmap.isRecycled())
        {
            bitmap.recycle();
        }
    }

    @Override
    public void draw(Canvas canvas)
    {
        ListAdapter adapter = getAdapter();
        // 当没有设置adapter，或者adapter返回的总数为0的时候，才进行无数据图像绘制
        if (adapter == null || adapter.getCount() == 0)
        {
            if (no_date_res != -1)
            {
                if (bitmap == null)
                {
                    bitmap = initBitmap();
                    postInvalidate();
                } else
                {
                    canvas.drawBitmap(bitmap, mWidth / 2 - imgWidth / 2f, mHeight / 2 - imgHeight / 2f, paint);
                }
            }
        }

        super.draw(canvas);
    }
}
